package com.kingzoon.seckill.service.impl.account;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import payment.tools.util.TimeUtil;

import com.kingzoon.seckill.dao.UaBankInfoMapper;
import com.kingzoon.seckill.dao.UaCapChannelMapper;
import com.kingzoon.seckill.dao.UaMemberInfoMapper;
import com.kingzoon.seckill.dto.Result;
import com.kingzoon.seckill.entity.UaBankInfo;
import com.kingzoon.seckill.entity.UaBankInfoExample;
import com.kingzoon.seckill.entity.UaCapChannel;
import com.kingzoon.seckill.entity.UaCapChannelExample;
import com.kingzoon.seckill.entity.UaMemberInfo;
import com.kingzoon.seckill.entity.UaMemberInfoExample;
import com.kingzoon.seckill.enums.MacroDefine;
import com.kingzoon.seckill.service.OpenAcctDepositService;
import com.kingzoon.seckill.util.UtilTools;

@Service
public class OpenAcctDepositServiceImpl implements OpenAcctDepositService {
	
	private HashMap<String,Object> paramMap = new HashMap<String,Object>();
	
	@Autowired
	private UaMemberInfoMapper uaMemberInfoDao;
	
	@Autowired
	private UaCapChannelMapper uaCapChannelDao;
	
	@Autowired
	private UaBankInfoMapper uaBankInfoDao;
	
	private UaMemberInfoExample uaMemberInfoExample = new UaMemberInfoExample();
	private UaCapChannelExample uaCapChannelExample = new UaCapChannelExample();
	private UaCapChannel uaCapChannel = new UaCapChannel();
	private UaBankInfoExample uaBankInfoExample = new UaBankInfoExample();
	private UaBankInfo uaBankInfo = new UaBankInfo();
	private UaMemberInfo uaMemberInfo = new UaMemberInfo();
	

	public Result<HashMap<String, Object>> doBusiness(HashMap<String, Object> mapParam, Result<HashMap<String, Object>> rs) {
		paramMap = mapParam;
		if(preCheck(rs)){
			if(cache(rs)){
				if(check(rs)){
					if(before(rs)){
						if(clear(rs)){
							after(rs);
						}
					}
				}
			}
		}
		return rs;
	}

	public boolean preCheck(Result<HashMap<String, Object>> rs) {
		String[] keys = {"exchangeId","distributorId","memberId","channelType","smsCode","bankNo","bankAcc","bindMode"};
		String emptyString = UtilTools.isParamEmpty(keys, paramMap);
		if(!UtilTools.isEmpty(emptyString)){
			rs.setFlag(false);
			rs.setCode(MacroDefine.GS_CODE.E_1002.getCode());
			rs.setMessage(MacroDefine.GS_CODE.E_1002.getMessage());
			return false;
		}
		return true;
	}

	public boolean cache(Result<HashMap<String, Object>> rs) {
		return true;
	}

	public boolean check(Result<HashMap<String, Object>> rs) {
		//校验会员信息表
		uaMemberInfoExample.createCriteria().andMemberIdEqualTo(UtilTools.valueOf(paramMap.get("memberId")));
		List<UaMemberInfo> memberInfoList = uaMemberInfoDao.selectByExample(uaMemberInfoExample);
		if(memberInfoList == null || memberInfoList.size() <= 0){
			rs.setFlag(false);
			rs.setCode(MacroDefine.ZH_CODE.E_5400.getCode());
			rs.setMessage(MacroDefine.ZH_CODE.E_5400.getMessage());
			return false;
		}
		uaMemberInfo = memberInfoList.get(0);
		
		//校验资金账户渠道关系表
		uaCapChannelExample.createCriteria().andMemberIdEqualTo(UtilTools.valueOf(paramMap.get("memberId")))
		.andChannelTypeEqualTo(UtilTools.valueOf(paramMap.get("channelType")));
		List<UaCapChannel> capChannelList = uaCapChannelDao.selectByExample(uaCapChannelExample);
		if(capChannelList == null || capChannelList.size() <= 0){
			rs.setFlag(false);
			rs.setCode(MacroDefine.ZH_CODE.E_5400.getCode());
			rs.setMessage(MacroDefine.ZH_CODE.E_5400.getMessage());
			return false;
		}
		uaCapChannel = capChannelList.get(0);
		
		//校验银行签约表
		String uniqueId = (String)paramMap.get("bankAcc") + (String)paramMap.get("bankNo") + (String)paramMap.get("channelType");
		List<String> statusList = new ArrayList<String>();
		statusList.add("1");
		statusList.add("2");
		uaBankInfoExample.createCriteria().andUniqueCodeEqualTo(uniqueId).andStatusIn(statusList);
		List<UaBankInfo> bankInfoList = uaBankInfoDao.selectByExample(uaBankInfoExample);
		if(bankInfoList != null && bankInfoList.size() > 0){
			rs.setFlag(false);
			rs.setCode(MacroDefine.ZH_CODE.E_5415.getCode());
			rs.setMessage(MacroDefine.ZH_CODE.E_5415.getMessage());
			return false;
		}
		return true;
	}

	public boolean before(Result<HashMap<String, Object>> rs) {
		String status = "2";//2-已绑定
		String availableWithdrawDay = "1";//提现T+1
		String payChannelName = "thirdpay";
		paramMap.put("memberId", uaMemberInfo.getMemberId());
		paramMap.put("capitalAccount", uaCapChannel.getCapitalAccount());
		paramMap.put("bankCode", paramMap.get("bankNo"));
		paramMap.put("bankAccount", paramMap.get("bankAcc"));
		paramMap.put("bankAccountName", uaMemberInfo.getMemberName());
		paramMap.put("phone", uaMemberInfo.getMemberMobile());
		paramMap.put("status", status);
		paramMap.put("insertDate", TimeUtil.getTimeStamp().substring(0, 8));
		paramMap.put("insertTime", TimeUtil.getTimeStamp().substring(8));
		paramMap.put("openBankAddress", paramMap.get("branch"));
		paramMap.put("openBankProvince", paramMap.get("province"));
		paramMap.put("openBankCity", paramMap.get("city"));
		paramMap.put("openBankCounty", paramMap.get("country"));
		paramMap.put("bindDate", paramMap.get("insertDate"));
		paramMap.put("bindTime", paramMap.get("insertTime"));
		paramMap.put("payChannelCode", (String)paramMap.get("bankNo") + (String)paramMap.get("channelType"));
		paramMap.put("payChannelName", payChannelName);
		paramMap.put("payChannelType", paramMap.get("channelType"));
		paramMap.put("uniqueCode", (String)paramMap.get("bankAcc") + (String)paramMap.get("payChannelCode"));
		paramMap.put("bindType", paramMap.get("bindMode"));
		paramMap.put("availableWithdrawDay", availableWithdrawDay);
		uaBankInfo.setExchangeId(UtilTools.valueOf(paramMap.get("exchangeId")));
		uaBankInfo.setDistributorId(UtilTools.valueOf(paramMap.get("distributorId")));
		uaBankInfo.setMemberId(UtilTools.valueOf(paramMap.get("memberId")));
		uaBankInfo.setCapitalAccount(UtilTools.valueOf(paramMap.get("capitalAccount")));
		uaBankInfo.setBankCode(UtilTools.valueOf(paramMap.get("bankCode")));
		uaBankInfo.setBankName(UtilTools.valueOf(paramMap.get("bankName")));
		uaBankInfo.setBankAccount(UtilTools.valueOf(paramMap.get("bankAccount")));
		uaBankInfo.setBankAccountName(UtilTools.valueOf(paramMap.get("bankAccountName")));
		uaBankInfo.setBankCapitalAccount(UtilTools.valueOf(paramMap.get("bankCapitalAccount")));
		uaBankInfo.setPhone(UtilTools.valueOf(paramMap.get("phone")));
		uaBankInfo.setMobile(UtilTools.valueOf(paramMap.get("mobile")));
		uaBankInfo.setStatus(UtilTools.valueOf(paramMap.get("status")));
		uaBankInfo.setInsertDate(Long.valueOf((String)paramMap.get("insertDate")));
		uaBankInfo.setInsertTime(Long.valueOf((String)paramMap.get("insertTime")));
		uaBankInfo.setOpenBankAddress(UtilTools.valueOf(paramMap.get("openBankAddress")));
		uaBankInfo.setOpenBankName(UtilTools.valueOf(paramMap.get("openBankName")));
		uaBankInfo.setOpenBankProvince(UtilTools.valueOf(paramMap.get("openBankProvince")));
		uaBankInfo.setOpenBankCity(UtilTools.valueOf(paramMap.get("openBankCity")));
		uaBankInfo.setOpenBankCountry(UtilTools.valueOf(paramMap.get("openBankCountry")));
		uaBankInfo.setBindDate(Long.valueOf((String)paramMap.get("bindDate")));
		uaBankInfo.setBindTime(Long.valueOf((String)paramMap.get("bindTime")));
		uaBankInfo.setPayChannelCode(UtilTools.valueOf(paramMap.get("payChannelCode")));
		uaBankInfo.setPayChannelName(UtilTools.valueOf(paramMap.get("payChannelName")));
		uaBankInfo.setPayChannelType(UtilTools.valueOf(paramMap.get("payChannelType")));
		uaBankInfo.setUniqueCode(UtilTools.valueOf(paramMap.get("uniqueCode")));
		uaBankInfo.setBindType(UtilTools.valueOf(paramMap.get("bindType")));
		uaBankInfo.setAvailableWithdrawDay(Short.valueOf((String)paramMap.get("availableWithdrawDay")));
		return true;
	}

	@Transactional
	public boolean clear(Result<HashMap<String, Object>> rs) {
		//TODO 账户验证
		HashMap<String,Object> reqMap = new HashMap<String, Object>();
		reqMap.put("bankNo", paramMap.get("bankNo"));
		reqMap.put("bankAcc", paramMap.get("bankAcc"));
		reqMap.put("clientName", uaMemberInfo.getMemberName());
		boolean flag = acctVerify(reqMap);
		if(!flag){
			rs.setFlag(false);
			rs.setCode(MacroDefine.ZH_CODE.E_5417.getCode());
			rs.setMessage(MacroDefine.ZH_CODE.E_5417.getMessage());
			return false;
		}
		//写入银行签约表
		int a = uaBankInfoDao.insertSelective(uaBankInfo);
		if(a != 1){
			rs.setFlag(false);
			rs.setCode(MacroDefine.JC_CODE.R_9999.getCode());
			rs.setMessage(MacroDefine.JC_CODE.R_9999.getMessage());
			return false;
		}
		return true;
	}

	public boolean after(Result<HashMap<String, Object>> rs) {
		HashMap<String, Object> respMap = new HashMap<String,Object>();
		respMap.put("exchangeId", uaBankInfo.getExchangeId());
		respMap.put("distributorId", uaBankInfo.getDistributorId());
		respMap.put("memberId", uaBankInfo.getMemberId());
		respMap.put("capitalAccount", uaBankInfo.getCapitalAccount());
		respMap.put("bankNo", uaBankInfo.getBankCode());
		respMap.put("bankName", uaBankInfo.getBankName());
		respMap.put("bankAcc", uaBankInfo.getBankAccount());
		respMap.put("clientName", uaBankInfo.getBankAccountName());
		respMap.put("mobile", uaBankInfo.getMobile());
		respMap.put("phone", uaBankInfo.getPhone());
		respMap.put("branch", uaBankInfo.getOpenBankAddress());
		respMap.put("province", uaBankInfo.getOpenBankProvince());
		respMap.put("city", uaBankInfo.getOpenBankCity());
		respMap.put("country", uaBankInfo.getOpenBankCountry());
		rs.setData(respMap);
		rs.setFlag(true);
		rs.setCode(MacroDefine.JC_CODE.S_0000.getCode());
		rs.setMessage(MacroDefine.JC_CODE.S_0000.getMessage());
		return true;
	}
	
	public boolean acctVerify(HashMap<String,Object> reqMap){
		//TODO 账户验证
		return true;
	}

}
